﻿using UnityEditor; 

namespace XFGameFramework.SkillSystem
{
    public class ListenerFileEvent : UnityEditor.AssetModificationProcessor
    { 
        public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions option)
        { 
            SkillsConfig skillsConfig = AssetDatabase.LoadAssetAtPath<SkillsConfig>(assetPath);

            if (skillsConfig != null)
            {

                for (int i = 0; i < skillsConfig.skills.Count; i++)
                {
                    if (skillsConfig.skills[i] == null) continue;
                    AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(skillsConfig.skills[i]));
                }

                string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(skillsConfig));
                string dir = string.Format("Assets/Extensions/SkillSystem/Skills/{0}", guid);
                AssetDatabase.DeleteAsset(dir);
            }

            return AssetDeleteResult.DidNotDelete; 
        }


        [UnityEditor.Callbacks.OnOpenAsset(0)]
        private static bool OnOpenAsset(int insId, int line)
        {
            SkillsConfig obj = EditorUtility.InstanceIDToObject(insId) as SkillsConfig;
            if (obj != null)
            {
                //EditorWindow.GetWindow<FSMEditorWindow>().Show();
                SkillsConfigInspector.OpenSkillsConfig(obj);
            }
            return obj != null;
        }

    }
}

